<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/23
 * Time: 15:16
 */

namespace app\index\controller;

use app\index\model\Delete as del;
use think\Db;

class Delete extends Common
{
	protected $del = '';
	public function _initialize()
	{
		parent::_initialize();
		$this->del = new del();
	}
	
	/**
	 * 已删除商品首页
	*/
	public function index()
	{
		return view('index');
		
	}
	
	/**
	 * 获取以删除的商品信息
	 * @param int get.rows 每页显示的条数
	 * @param int get.page 第几页
	 * @param int get.title 需要检索的信息
	*/
	public function getInfo()
	{
		$rows = input('get.rows');
		$page = input('get.page');
		$where = [];
		if(!empty(input('get.title'))){
			$title = input('get.title');
			$where['barcode'] = ['like',"%{$title}%"];
			$where['pname'] = ['like',"%{$title}%"];
		}
		$count = $this->del->whereOr($where)->where('status',2)->count();
		$result = $this->del->whereOr($where)->where('status',2)->limit($rows)->page($page)->select();
		return json(['rows'=>$result,'total'=>$count]);
	}
	/**
	 * 批量恢复
	 * @param array get.id  需要恢复的商品id
	*/
	public function recovery()
	{
		try{
			if(request()->isAjax()){
				Db::startTrans();
				$id = request()->param('id');
				$result = $this->del->whereIn('id',$id)->update(['status'=>7]);
				if(!$result){
					throw new \PDOException('恢复失败');
				}
				Db::commit();
				$success = ['success'=>'恢复成功'];
				return json($success);
			}
		}catch (\PDOException $e){
				Db::rollback();
				$error = ['error'=>$e->getMessage()];
				return json($error);
		}
	}
	
	/**
	 * 删除商品
	 * @param array get.id  需要删除的商品id
	*/
	public function delete()
	{
		try{
			if(request()->isAjax()){
				Db::startTrans();
				$id = request()->param('id');
				$result = $this->del->whereIn('id',$id)->delete();
				if(!$result){
					throw new \PDOException('删除失败');
				}
				Db::commit();
				$success = ['success'=>'删除成功'];
				return json($success);
			}
		}catch (\PDOException $e){
			Db::rollback();
			$error = ['error'=>$e->getMessage()];
			return json($error);
		}
	}
}